ig.module(

    'game.entities.factory'

).requires(
           
    'impact.entity',
    'game.entities.particle',
    'game.entities.blob',
    'game.entities.kami'

).defines(function(){
    
    EntityFactory = ig.Entity.extend({
        size: {
            x: 12,
            y: 12
        },
        offset: {
            x: 2,
            y: 4
        },
        maxVel: {
            x: 1,
            y: 1
        },
        type: ig.Entity.TYPE.B,
        checkAgainst: ig.Entity.TYPE.A,
        collides: ig.Entity.COLLIDES.PASSIVE,
        health: 20,
        
        bornTimer: null,
        
        bornPeriod: 2,
        
        seeDistance: 100,
        
        seePlayer: false,
        
        animSheet: new ig.AnimationSheet('media/sprites/factory.png', 16, 16),
        
        init: function(x,y,settings){
            
            this.parent( x,y,settings );
            
            this.bornTimer = new ig.Timer();
            this.bornTimer.set( this.bornPeriod );
            
            this.addAnim('idle', 1, [0]);
            
        },
        
        update: function(){

            this.seePlayer = ( this.distanceTo(ig.game.player) < this.seeDistance );
                
            if( this.seePlayer ){
                if( this.bornTimer.delta() > 0 ){                
                    ig.game.spawnEntity(EntityKami, this.pos.x, this.pos.y, {
                        seenPlayer: true });
                    this.bornTimer.set( this.bornPeriod );
                }
            }            
            
            this.parent();
        },
        
        kill: function(){
            var gibs = ig.ua.mobile ? 5 : 160;
            for (var i = 0; i < gibs; i++) {
                ig.game.spawnEntity(EntityFactoryGib, this.pos.x, this.pos.y);
            }
            this.parent();
        }        
    });
    
    EntityFactoryGib = EntityParticle.extend({
        lifetime: 3,
        fadetime: 6,
        friction: {
            x: 0,
            y: 0
        },
        vel: {
            x: 60,
            y: 150
        },
        animSheet: new ig.AnimationSheet('media/sprites/blob-gibs.png', 4, 4),
        init: function (x, y, settings) {
            this.addAnim('idle', 0.1, [0, 1, 2]);
            this.parent(x, y, settings);
        }
    });
    
    
});